package com.wanmait.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wanmait.schedule.entity.CompanyUser;
import com.wanmait.schedule.entity.Department;
import com.wanmait.schedule.entity.Position;
import com.wanmait.schedule.entity.Userinfo;
import com.wanmait.schedule.mapper.CompanyUserMapper;
import com.wanmait.schedule.mapper.DepartmentMapper;
import com.wanmait.schedule.mapper.PositionMapper;
import com.wanmait.schedule.mapper.UserinfoMapper;
import com.wanmait.schedule.service.CompanyUserService;
import com.wanmait.schedule.service.PositionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 职位表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-03-02
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {
    @Resource
    private CompanyUserMapper companyUserMapper;
    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private PositionMapper positionMapper;

    @Override
    public List<Department> initData(Integer companyId) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getCompanyId,companyId);
        List<Department> departments = departmentMapper.selectList(queryWrapper);
        //LambdaQueryWrapper<CompanyUser> queryWrapper1 = new LambdaQueryWrapper<>();
        Map<String, Object> map = new HashMap<>();
        departments.forEach(department -> {
            map.put("department_id",department.getId());
            //查找到部门下的所有员工
            department.setCompanyUserList(companyUserMapper.selectByMap(map));
            //companyUserList中有userinfo对象
            for (int i=0;i<department.getCompanyUserList().size();i++){
                if (department.getCompanyUserList().get(i).getPositionId() != 2){
                    continue;
                }
                department.getCompanyUserList().get(i).setUserinfo(userinfoMapper.selectById(department.getCompanyUserList().get(i).getUserinfoId()));
                department.getCompanyUserList().get(i).setPosition(positionMapper.selectById(department.getCompanyUserList().get(i).getPositionId()));
            }
        });
        return departments;
    }
}
